package com.tring.ysyn.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.tring.ysyn.entity.SysUser;
import com.tring.ysyn.excel.vo.UserExcelVo;
import com.tring.ysyn.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Tring
 * date 2022-11-08
 */
@Component
public class ExcelBaseListener extends AnalysisEventListener<UserExcelVo> {


    private ISysUserService userService;

    public ExcelBaseListener(ISysUserService userService) {
        this.userService = userService;
    }

    public ExcelBaseListener() {
    }

    @Override
    public void onException(Exception e, AnalysisContext analysisContext) throws Exception {

    }


    /**
     * 这个每一条数据解析都会来调用
     *数据检验
     * @param vo
     * @param analysisContext
     */
    @Override
    @Transient
    public void invoke(UserExcelVo vo, AnalysisContext analysisContext) {
        SysUser user = new SysUser();
        //入库
        BeanUtils.copyProperties(vo, user);
        user.getUserName();
        userService.save(user);
        System.out.println(vo.toString());
    }


    /**
     * 所有数据解析完成了 都会来调用
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //log.info("收尾工作" + analysisContext);
    }

}
